import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:mirror/AppStyle.dart';
import 'package:mirror/Util.dart';
import 'package:mirror/api/UserApi.dart';
import 'package:mirror/bean/UserBean.dart';
import 'package:mirror/page/LoginPage.dart';
import 'package:mirror/page/home/SettingPage.dart';
import 'package:mirror/service/ProviderService.dart';
import 'package:mirror/widget/AvatarWidget.dart';
import 'package:provider/provider.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  List<String> func = [
    "功能1",
    "功能1",
    "退出登录",
  ];
  UserBean? user;

  @override
  void initState() {
    super.initState();
    refresh();
  }

  @override
  Widget build(BuildContext context) {
    var myData = Provider.of<ProviderData>(context);
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  InkWell(
                    onTap: () async {
                      await Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (c) => const SettingPage()));
                      refresh();
                    },
                    child: const Padding(
                        padding: EdgeInsets.all(8),
                        child: Icon(Icons.settings)),
                  )
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
              child: Container(
                decoration: BoxDecoration(
                    borderRadius: Constant.borderRadius,
                    image: const DecorationImage(
                        image: NetworkImage(
                            "https://img.99ppt.com/pic/b64d1767-1871-4a38-a4d5-227f4c309e9f.jpg"),
                        fit: BoxFit.cover)),
                child: ClipRect(
                  child: BackdropFilter(
                    filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
                    child: Column(
                      children: [
                        Container(
                          decoration: const BoxDecoration(
                              // color: Colors.orangeAccent.shade100,
                              borderRadius: BorderRadius.vertical(
                                  top: Radius.circular(5))),
                          padding: const EdgeInsets.symmetric(
                              vertical: 10, horizontal: 20),
                          child: Row(
                            children: [
                              AvatarWidget(
                                imageUrl: myData.userAvatar.isEmpty
                                    ? "${user?.userAvatar}"
                                    : myData.userAvatar,
                                size: 50,
                                circular: 30,
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    "${user?.nickName}",
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        overflow: TextOverflow.ellipsis),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      Util.copy("${user?.userUid}");
                                    },
                                    child: Padding(
                                      padding: const EdgeInsets.symmetric(
                                          vertical: 3),
                                      child: Row(
                                        children: [
                                          Text(
                                            "UID:${user?.userUid}",
                                            overflow: TextOverflow.ellipsis,
                                            style:
                                                const TextStyle(fontSize: 10),
                                          ),
                                          const SizedBox(
                                            width: 10,
                                          ),
                                          const Icon(
                                            Icons.copy,
                                            size: 15,
                                          )
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              )
                            ],
                          ),
                        ),
                        Container(
                          padding: const EdgeInsets.only(
                              bottom: 10, left: 10, right: 10),
                          decoration: const BoxDecoration(
                              borderRadius: BorderRadius.vertical(
                                  bottom: Radius.circular(5))),
                          child: Column(
                            children: [
                              InkWell(
                                onTap: () {
                                  Util.copy("${user?.chanAddress}");
                                },
                                child: Padding(
                                  padding:
                                      const EdgeInsets.symmetric(vertical: 3),
                                  child: Row(
                                    children: [
                                      const Text(
                                        "区块链地址:",
                                        style: TextStyle(fontSize: 10),
                                      ),
                                      Expanded(
                                          child: Text(
                                        "${user?.chanAddress}",
                                        overflow: TextOverflow.ellipsis,
                                        style: const TextStyle(
                                            fontSize: 10,
                                            fontWeight: FontWeight.bold),
                                      )),
                                      const Icon(
                                        Icons.copy,
                                        size: 15,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Container(
                                padding:
                                    const EdgeInsets.symmetric(vertical: 10),
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                            vertical: 10),
                                        decoration: BoxDecoration(
                                            color: Colors.white,
                                            borderRadius: Constant.borderRadius,
                                            boxShadow: Constant.boxShadow),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: const [
                                            Icon(Icons
                                                .dashboard_customize_outlined),
                                            SizedBox(
                                              width: 5,
                                            ),
                                            Text("我的藏品")
                                          ],
                                        ),
                                      ),
                                    ),
                                    const SizedBox(
                                      width: 10,
                                    ),
                                    Expanded(
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                            vertical: 10),
                                        decoration: BoxDecoration(
                                            color: Colors.white,
                                            borderRadius: Constant.borderRadius,
                                            boxShadow: Constant.boxShadow),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: const [
                                            Icon(
                                                Icons.monetization_on_outlined),
                                            SizedBox(
                                              width: 5,
                                            ),
                                            Text("进入钱包")
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: Constant.borderRadius,
                    boxShadow: Constant.boxShadow),
                child: ListView.builder(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: 4,
                    itemBuilder: (c, i) {
                      return InkWell(
                        onTap: () {},
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 20, vertical: 15),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: const [
                                  Icon(Icons.reorder),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text("我的订单"),
                                ],
                              ),
                              const Icon(Icons.keyboard_arrow_right_outlined)
                            ],
                          ),
                        ),
                      );
                    }),
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: Constant.borderRadius,
                    boxShadow: Constant.boxShadow),
                child: ListView.builder(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: func.length,
                    itemBuilder: (c, i) {
                      return InkWell(
                        onTap: () {
                          switch (func[i]) {
                            case "退出登录":
                              Navigator.pushReplacement(
                                  context,
                                  MaterialPageRoute(
                                      builder: (c) => const LoginPage()));
                              break;
                          }
                        },
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 20, vertical: 15),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  const Icon(Icons.reorder),
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  Text(func[i]),
                                ],
                              ),
                              const Icon(Icons.keyboard_arrow_right_outlined)
                            ],
                          ),
                        ),
                      );
                    }),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> refresh() async {
    user = await UserApi.getUser();
    setState(() {});
  }
}
